package com.ys.utils.spring.base;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;

@Component
public class SpringTool implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    public static <T> T getBean(Class<T> tClass) {
        return applicationContext.getBean(tClass);
    }
    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }
    public static <T> Collection<T> getBeans(Class<T> tClass) {
        Map<String, T> beansOfType = applicationContext.getBeansOfType(tClass);
        return beansOfType.values();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringTool.applicationContext = applicationContext;
    }
}
